package managedBeans;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import negocio.TweetBO;
import entidades.Tweet;

@ManagedBean
@ViewScoped
public class TweetMB implements Serializable {

	private static final long serialVersionUID = 1L;

	private TweetBO business;

	private Tweet tweet;

	public TweetMB() {
		business = new TweetBO();
		tweet = new Tweet();
	}

	public void inserir() {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		Map<String, Object> map = facesContext.getExternalContext()
				.getSessionMap();

		UsuarioMB uMB = (UsuarioMB) map.get("usuarioMB");

		tweet.setUsuario(uMB.getUsuario());
		tweet.setData(new Timestamp(new Date().getTime()));

		business.inserir(uMB.getUsuario(), tweet);
		tweet = new Tweet();
	}

	public void remover(Tweet tweet) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Map<String, Object> map = facesContext.getExternalContext()
				.getSessionMap();

		UsuarioMB uMB = (UsuarioMB) map.get("usuarioMB");

		business.remover(uMB.getUsuario(), tweet);
	}

	public void retweetar(Tweet tweet) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Map<String, Object> map = facesContext.getExternalContext()
				.getSessionMap();

		UsuarioMB uMB = (UsuarioMB) map.get("usuarioMB");

		business.retweetar(uMB.getUsuario(), tweet);
	}

	public Tweet getTweet() {
		return tweet;
	}

	public void setTweet(Tweet tweet) {
		this.tweet = tweet;
	}

}
